********************************************************************************************************
**** Multilevel regressions: Nationalist attitude mediates effect on EU support (Appendix C.8) *********
********************************************************************************************************
clear
use "BHPS_merged.dta"

do "ML programs to add model statistics.do"			

* global with individual-level control variables
	global controls "i.education i.male c.age_cent##c.age_cent i.bornelsewhere2 i.parentsbornelsewhere"


******************* Create necessary change in nationalist attitude variable (lagged one year for 2006)	
	gen L1_D3_nationalism=L1.D3_nationalism 
	
	gen D_nationalism_forEU=D3_nationalism if year==2002					
	replace D_nationalism_forEU=L1_D3_nationalism if year==2006					
	
	
*** Regressions
	global tradevar "real_gr_imp_CH_d_eu_nl"		

	* without controls
	mixed D_EUsupport c.LDV_EUsupport##i.year ///
		D_nationalism_forEU ///
		$tradevar ///
		$controls i.ID_NUTS1xYear ///
		if (year==2006 | year==2002) ///
		|| ID_NUTS3: || ID_NUTS3xYear: , stddeviations 

		add_scalars_mixed_2level
		sum D_EUsupport $tradevar if e(sample)
														
		eststo EU1	

	* with controls: employment share				
	mixed D_EUsupport c.LDV_EUsupport##i.year ///
		D_nationalism_forEU ///
		$tradevar ///
		emplsh_manufact_1998 ///
		$controls i.ID_NUTS1xYear ///
		if (year==2006 | year==2002) ///
		|| ID_NUTS3: || ID_NUTS3xYear: , stddeviations 

		add_scalars_mixed_2level
		sum D_EUsupport $tradevar if e(sample)
		
		eststo EU2	

	* with all controls				
	mixed D_EUsupport c.LDV_EUsupport##i.year ///
		D_nationalism_forEU ///
		$tradevar ///
		emplsh_manufact_1998 pop_sharenonUK pop_sharenonUK_d_eu ///
		$controls i.ID_NUTS1xYear  ///
		if (year==2006 | year==2002) ///
		|| ID_NUTS3: || ID_NUTS3xYear: || ID_lad13xyear: , stddeviations

		add_scalars_mixed_3level						
					
		eststo EU3	
	
	* with all controls & NUTS2-year FE						
	mixed D_EUsupport c.LDV_EUsupport##i.year ///
		D_nationalism_forEU ///
		$tradevar ///
		emplsh_manufact_1998 pop_sharenonUK pop_sharenonUK_d_eu ///
		$controls i.ID_NUTS2xYear  ///
		if (year==2006 | year==2002) ///
		|| ID_NUTS3: || ID_NUTS3xYear: || ID_lad13xyear: , stddeviations

		add_scalars_mixed_3level
		sum D_EUsupport $tradevar if e(sample)
		
		eststo EU4		
			

* write table
	esttab EU1 EU2 EU3 EU4 using "ML regressions nationalism as mediator.rtf", b(a2) se(a2) star(+ 0.10 * 0.05 ** 0.01 *** 0.001) ///
	scalars(group1N group2N group3N ri1_std ri2_std ri3_std icc1 icc2 icc3 bic) varwidth(30) nogaps compress ///
	drop(0.male 0.education 0.bornelsewhere2 0.parentsbornelsewhere 2002.year *.ID_NUTS1xYear *.ID_NUTS2xYear lnsig_e:_cons) ///
	label replace transform(ln*: exp(@) exp(@)) 	
		